package maple.util;

import java.util.Iterator;
import java.util.Vector;

import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import maple.lang.Language;

public class Service extends Object
{
	public static enum Property{ NAME, TYPE, OWNERSHIP };

	/**
	 * return the service description under some specified conditions
	 *
	 * @param	name	the specified name
	 * @param	type	the specified type
	 * @return	the service description under some specified conditions
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public static ServiceDescription getInstance( final String name, final String type )
	{
		ServiceDescription service = new ServiceDescription();
		service.addLanguages( Language.class.getName() );
		service.setName( name );
		service.setType( type );
		return service;
	}

	/**
	 * return the operators of the specified services
	 *
	 * @param	services	the specified services
	 * @return	the operators of the specified services
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public static Vector< String > toOperators( final Vector< ServiceDescription > services )
	{
		Vector< String > operators = new Vector< String >();
		for( ServiceDescription service : services )
		{
			String operator = "";
			String postcondition = "";
			String precondition = "";

			Iterator properties = service.getAllProperties();
			while( properties.hasNext() )
			{
				jade.domain.FIPAAgentManagement.Property property = ( jade.domain.FIPAAgentManagement.Property ) properties.next();
				String name = property.getName();
				if( name.equals( Planner.Property.OPERATOR.toString() ) )
				{
					 operator = ( String )property.getValue();
				}
				else if( name.equals( Planner.Property.PRECONDITION.toString() ) )
				{
					precondition = ( String )property.getValue();
				}
				else if( name.equals( Planner.Property.POSTCONDITION.toString() ) )
				{
					postcondition = ( String )property.getValue();
				}
			}
			operators.add( operator
						   + Language.TAB + precondition
						   + Language.TAB + postcondition );
			//System.out.println( "OPERATOR: " + operator
			//			   + Language.TAB + precondition
			//			   + Language.TAB + postcondition );
		}
		return operators;
	}

	/**
	 * return the string representation of the specified service description
	 *
	 * @param	service	the specified service description
	 * @return	the string representation of the specified description
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public static String toString( final ServiceDescription service )
	{
		String string = Service.Property.NAME + Language.ASSIGN
						+ Language.QUOTE
						+ service.getName()
						+ Language.QUOTE
						+ Language.DELIMITER 
						+ Service.Property.TYPE + Language.ASSIGN
						+ Language.QUOTE
						+ service.getType() 
						+ Language.QUOTE
						+ Language.DELIMITER 
						+ Service.Property.OWNERSHIP + Language.ASSIGN
						+ Language.QUOTE
						+ service.getOwnership()
						+ Language.QUOTE;

		//Iterator iter = service.getAllProperties();
		//while( iter.hasNext() )
		//{
		//	jade.domain.FIPAAgentManagement.Property property = ( jade.domain.FIPAAgentManagement.Property ) iter.next();
		//	string = string + Language.DELIMITER
		//			 + property.getName() + Language.ASSIGN
		//			 + Language.QUOTE
		//			 + ( String ) property.getValue()
		//			 + Language.QUOTE;
		//}

		return string;
	}
}
